<?php
/**
 * @version $Id: view.html.php 48 2011-09-28 06:47:49Z lnmtrung $
 */
 
defined( '_JEXEC' ) or die( 'Restricted access' );
jimport( 'joomla.application.component.view' );

class WrdViewMservices extends WrdView{
	
	function display($tpl = null) {
		$this->setModel($this->getModel('mservices'), true);
		$fnc = 'display'.ucfirst($this->getLayout());
		if (is_callable(array(&$this, $fnc)))
			$this->$fnc($tpl);
		else
			parent::display($tpl);
	}

	function displayDefault($tpl = null){
		$mainframe = &JFactory::getApplication();
		$context = JRequest::getCmd('option');
		$view = JRequest::getCmd('view');
		
		$document = & JFactory::getDocument();
		$document->setTitle( JText::_('Member Services') );

		JToolBarHelper::title(JText::_('Member Services'),'mservices.png');
 		JToolBarHelper::addNew();
 		JToolBarHelper::publish();
 		JToolBarHelper::unpublish();
		JToolBarHelper::deleteList();
		
		WrdHelper::addSubMenu($view);
		$this->sidebar = JHtmlSidebar::render();
		
		$filter_order     		= $mainframe->getUserStateFromRequest( $context.$view.'filter_order_item','filter_order','id' );
		$filter_order_Dir 		= $mainframe->getUserStateFromRequest( $context.$view.'filter_order_Dir','filter_order_Dir', '' );

		$lists['order'] 		= $filter_order;
		$lists['order_Dir'] 	= $filter_order_Dir;
		
		$items					= & $this->get( 'Data');
		$user_profile = array() ;
		foreach ($items as $item){
			$user_profile[$item->vip_number] = WrdHelper::getUserProfile($item->vip_number, 'member_id') ; 
		}
		$total					= & $this->get( 'Total');
		$pagination 			= & $this->get( 'Pagination' );
		 
		$search 				= $mainframe->getUserStateFromRequest( $context.$view.'search','search','','string' );
		$filter_outlet 			= $mainframe->getUserStateFromRequest( $context.$view.'o_code','o_code',-1,'int' );
		
		$search 				= JString::strtolower( $search );
		
		$lists['search'] 		= $search;
		$lists['outlets'] 		= JHTML::_('wrd.listOutlets','o_code',$filter_outlet,'onchange="this.form.submit()"','o_code','', ' and pid != "00" AND `code` != ""  ' );
		
	    $this->assignRef('search',		$search);
	    $this->assignRef('lists',		$lists);
	  	$this->assignRef('items',		$items);
	    $this->assignRef('user_profile',	$user_profile);
	    $this->assignRef('pagination',	$pagination);
		parent::display($tpl);
  	}
  
  	function displayForm($tpl = null){
		$mainframe = &JFactory::getApplication();
		$context = JRequest::getCmd('option');
		$view = JRequest::getCmd('view');
		$user = JFactory::getUser();
		$cid = JRequest::getInt('id');
		$model	=& $this->getModel();
		$model_outlet = &JModelLegacy::getInstance('outlets','WrdModel');
        		
		$detail = array();
		if($cid != 0)
		{
			$detail	=& $model->getItem($cid);
		}
		
		$mid = JRequest::getInt('vip_num');
		if(empty($mid)){
			 $mid = $detail->vip_number;
			 $forward = 0;
		}else{
			$forward = 1;
		}
		$user_info = null ;
		
  		if(JRequest::getVar('tmpl') != component ){
			$user_info = WrdHelper::getUserProfile($mid, 'memberid') ; 
			$tmpl_flag = 0 ;
		}else{
			$tmpl_flag = 1 ;
		}
		
		$l_tran = array() ;
		$l_tran_outlet = array() ; 
  		if(!empty($user_info->user_id)){
			$outlet = $model_outlet->getItem($user_info->pick_up_at);
			$p_o_u = WrdHelper::getPoints($user_info->user_id);
  			if($user_info->job != 'other'){
				$job = $this->getJobName($user_info->job) ;
			}else{
				$job = $user_info->o_job ;
			}
			$l_tran = $this->getLastTransaction($user_info->member_id);
			$l_tran_outlet = $this->getOutletByCode($l_tran->outlet);
		}
		$lists = array();
		$lists['outlets'] 		= JHTML::_('wrd.listOutlets','o_code',$detail->o_code,'','o_code','', ' and pid != "00" AND `code` != ""  ' );
		//$lists['question'] 		= JHTML::_('wrd.listMsquestions','questions',$detail->questions,'',0 );
		$lists['meal_period'] 		= JHTML::_('wrd.meal_period',$detail->meal_period,'',0 );
		$wysiwyg = & JFactory::getEditor('tinymce');
		$questions = $wysiwyg->display('questions', htmlspecialchars($detail->questions, ENT_COMPAT, 'UTF-8'),  '100%', '200', '40', '5',false);
		$lists['questions'] = $questions;
		$answer = $wysiwyg->display('answer', htmlspecialchars($detail->answer, ENT_COMPAT, 'UTF-8'),  '100%', '200', '40', '5',false);
		$lists['answer'] = $answer;
		$follow_up = $wysiwyg->display('follow_up', htmlspecialchars($detail->follow_up, ENT_COMPAT, 'UTF-8'),  '100%', '200', '40', '5',false);
		$lists['follow_up'] = $follow_up;
		
		$text = ($detail->id < 1) ? JText::_( 'NEW' ) : JText::_( 'EDIT' );
		JToolBarHelper::title(   JText::_( 'Member Services' ).': <small><small>['.$text.']</small></small>','mservices.png' );
		if($user->authorise('wrd.profile.memberservices.add', 'com_wrd'))
			JToolBarHelper::save();
		JToolBarHelper::cancel( 'cancel', 'Close' );
		if($user->authorise('wrd.dashboard.admin', 'com_wrd'))
			JToolBarHelper::preferences('com_wrd','profile', '600');
		
		$this->assignRef('data',	$detail);
		$this->assignRef('lists',	$lists);
		$this->assignRef('user',	$user);
		$this->assignRef('user_info',	$user_info);
		$this->assignRef('p_o_u',	$p_o_u);
		$this->assignRef('outlet',	$outlet);
		$this->assignRef('forward',	$forward);
		$this->assignRef('l_tran',	$l_tran);
		$this->assignRef('l_tran_outlet',	$l_tran_outlet);
		$this->assignRef('job',$job);
		$this->assignRef('tmpl_flag',$tmpl_flag);
		parent::display($tpl);
	}
	function getJobName($id = 0){
		$db = JFactory::getDbo();
		$query = 'SELECT name FROM #__wrd_job WHERE id ='.$id;
		$db->setQuery($query);
		$rs = $db->loadObject();
		return $rs->name;
	}
	function getLastTransaction($vip_num = 0){
		$db = JFactory::getDbo();
		$query = 'SELECT * FROM #__wrd_transaction WHERE vip_number ='.$vip_num.' order by date_receipt desc limit 0,1';
		$db->setQuery($query);
		return $rs = $db->loadObject();
	}
	function getOutletByCode($o_code = 0){
		$db = JFactory::getDbo();
		$query = 'SELECT * FROM #__wrd_outlets WHERE code = '.$o_code.' and pid != 00 ' ;
		$db->setQuery($query);
		return $rs = $db->loadObject();
	}
}
?>